using System.IO;
using System.Threading.Tasks;
using MessagePack;
using UnityEngine;


[MessagePackObject]
public struct Header
{
    [Key(0)] public int Version { get; set; }
}

[MessagePackObject]
public class TestStruct
{
    [Key(0)] public Header Header { get; set; }
    [Key(1)] public string Name { get; set; }
}

public class TestMessagePack : MonoBehaviour
{
    private async void Start()
    {
        await Load();
    }

    private async ValueTask Save()
    {
        var msg = new TestStruct
        {
            Header = new Header
            {
                Version = 1
            },
            Name = "Pipi"
        };

        var fs = File.Open("user_archive.data", FileMode.OpenOrCreate);
        await MessagePackSerializer.SerializeAsync(fs, msg);
        fs.Close();
    }

    private async ValueTask Load()
    {
        var fs = File.Open("user_archive.data", FileMode.OpenOrCreate);
        var msg = await MessagePackSerializer.DeserializeAsync<TestStruct>(fs);
        fs.Close();
    }
}